package drazing;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Dictionary;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


/**
 * Dialogo de SET-UP del coche
 * @author xcubo
 */
public class SettingDiag extends JDialog
{
    protected int scrw,scrh;
    protected int wid = 333, hei = 500;
    private int left, up;

    protected JButton exitButton;
    protected JButton playButton;

    protected JTextField nombre;

    protected JRadioButton ferrari;
    protected JRadioButton mclaren;
    protected JRadioButton renault;
    protected ButtonGroup team;
    protected JLabel teampic;

    protected JSlider gear;
    protected int gfact;

    protected JSlider aero;
    protected JLabel carga;

    protected JRadioButton soft;
    protected JRadioButton hard;
    protected ButtonGroup tires;

    protected JSlider depo;
    protected JLabel  fuel;

    protected Image im, icon;

    protected Settings setup;

    public SettingDiag(Dimension dim)
    {
        scrw = dim.width;                                           // Tamaño de dialogo
        scrh = dim.height;
        left= (scrw/2) - (wid/2);
        up  = (scrh/2)- (hei/2);

        int row = 0;

        setup = new Settings();
        setup.setName("Unnamed");
        setup.setTeam("Ferrari");
        setup.setGear(0);
        setup.setAero(0.7);
        setup.setTire(false);
        setup.setFuel(24);


        setLayout(new GridBagLayout());

        JPanel rowName = new JPanel();
        JPanel rowHead = new JPanel();
        JPanel rowTeam = new JPanel();
        JPanel rowGear = new JPanel();
        JPanel rowAero = new JPanel();
        JPanel rowTire = new JPanel();
        JPanel rowFuel = new JPanel();


        GridBagConstraints rowconst = new GridBagConstraints();
        GridBagConstraints diaconst = new GridBagConstraints();
        rowconst.gridwidth = 1;
        rowconst.gridheight = 1;

        ////////////////////////////////////////////////////////////////////////Cabecera
        diaconst.gridx = 0;
        diaconst.gridy = row;
        diaconst.anchor = GridBagConstraints.NORTHWEST;
        row++;
        try {icon = ImageIO.read(getClass().getResource("../resources/img/icon/setup.png"));}
        catch (IOException e) {}
        ImageIcon leftico = new ImageIcon(icon.getScaledInstance(40, 40, Image.SCALE_SMOOTH));
        add(new JLabel(leftico),diaconst);

        rowHead.add(new JLabel("CONFIGURACIÓN DEL COCHE"));
        rowHead.setBorder(BorderFactory.createEmptyBorder(0,0,30,0));
        diaconst.gridx = 1;
        add(rowHead, diaconst);

        ////////////////////////////////////////////////////////////////////////Nombre
        rowName.setLayout(new GridBagLayout());
        rowconst = new GridBagConstraints();
        rowconst.gridx = 0;
        rowconst.gridy = 0;
        rowconst.anchor = GridBagConstraints.NORTHWEST;
        rowconst.gridwidth = 4;
        rowName.add(new JLabel("Nombre del jugador:"), rowconst);

        nombre = new JTextField(setup.getName(),20);                      // Creacion del text field

        rowconst.gridwidth = 1;
        rowconst.gridx = 0;
        rowconst.gridy = 1;
        rowName.add(nombre, rowconst);

        rowName.setBorder(BorderFactory.createEmptyBorder(0,0,10,0));

        diaconst.gridx = 0;
        diaconst.gridy = row;
        row++;
        try {icon = ImageIO.read(getClass().getResource("../resources/img/icon/name.png"));}
        catch (IOException e) {}
        leftico = new ImageIcon(icon.getScaledInstance(50, 50, Image.SCALE_SMOOTH));
        add(new JLabel(leftico),diaconst);

        diaconst.gridx = 1;
        add(rowName,diaconst);

        ////////////////////////////////////////////////////////////////////////Selector de equipo
        rowTeam.setLayout(new GridBagLayout());
        rowconst = new GridBagConstraints();
        diaconst.anchor = GridBagConstraints.CENTER;
        rowconst.gridx = 0;
        rowconst.gridy = 0;
        rowconst.anchor = GridBagConstraints.NORTHWEST;
        rowconst.gridwidth = 4;
        rowTeam.add(new JLabel("Selecciona un equipo:"), rowconst);

        ferrari = new JRadioButton("Ferrari");
        ferrari.setSelected(true);
        ferrari.setActionCommand("Ferrari");
        ferrari.addActionListener(new TeamListener(this));
        mclaren = new JRadioButton("McLaren");
        mclaren.setActionCommand("McLaren");
        mclaren.addActionListener(new TeamListener(this));
        renault = new JRadioButton("Renault");
        renault.setActionCommand("Renault");
        renault.addActionListener(new TeamListener(this));

        try {im = ImageIO.read(getClass().getResource("../resources/img/icon/Ferrari.png"));}
        catch (IOException e) {}
        ImageIcon ico = new ImageIcon(im.getScaledInstance(50, 50, Image.SCALE_SMOOTH));
        teampic = new JLabel(ico);

        team = new ButtonGroup();
        team.add(ferrari);
        team.add(mclaren);
        team.add(renault);

        rowconst.gridwidth = 1;
        rowconst.gridx = 0;
        rowconst.gridy = 1;
        rowconst.anchor = GridBagConstraints.CENTER;
        rowTeam.add(ferrari, rowconst);

        rowconst.gridx = 1;
        rowconst.gridy = 1;
        rowTeam.add(mclaren, rowconst);

        rowconst.gridx = 2;
        rowconst.gridy = 1;
        rowTeam.add(renault, rowconst);

        rowconst.gridx = 1;
        rowconst.gridy = 2;
        rowTeam.add(teampic, rowconst);
        rowTeam.setBorder(BorderFactory.createEmptyBorder(0,0,10,0));


        diaconst.gridx = 0;
        diaconst.gridy = row;
        row++;
        try {icon = ImageIO.read(getClass().getResource("../resources/img/icon/team.png"));}
        catch (IOException e) {}
        leftico = new ImageIcon(icon.getScaledInstance(50, 50, Image.SCALE_SMOOTH));
        add(new JLabel(leftico),diaconst);

        diaconst.gridx = 1;
        add(rowTeam, diaconst);
        ////////////////////////////////////////////////////////////////////////Selector de cambio
        rowGear.setLayout(new GridBagLayout());
        rowconst = new GridBagConstraints();
        rowconst.gridx = 0;
        rowconst.gridy = 0;
        rowconst.anchor = GridBagConstraints.NORTHWEST;
        rowconst.gridwidth = 4;
        rowGear.add(new JLabel("Configura la caja de cambios:"), rowconst);

        gear = new JSlider (JSlider.HORIZONTAL, 0, 4, setup.getGear());
        gear.setMajorTickSpacing(5);
        gear.setMinorTickSpacing(1);
        gear.setPaintTicks(true);
        gear.setPaintLabels(true);
        Hashtable hs = new Hashtable();
        Dictionary labelTable = hs;
        labelTable.put( new Integer( 0 ), new JLabel("Corta") );
        labelTable.put( new Integer( 2 ), new JLabel("Media") );
        labelTable.put( new Integer( 4 ), new JLabel("Larga") );
        gear.setLabelTable( labelTable );
        gear.addChangeListener(new GearSlider(this));

        rowconst.gridx = 0;
        rowconst.gridy = 1;
        rowconst.gridwidth = 2;
        rowconst.weightx = 0.0;
        rowconst.anchor = GridBagConstraints.CENTER;
        rowGear.add(gear, rowconst);
        rowGear.setBorder(BorderFactory.createEmptyBorder(0,0,10,0));

        diaconst.gridx = 0;
        diaconst.gridy = row;
        row++;
        try {icon = ImageIO.read(getClass().getResource("../resources/img/icon/gear.png"));}
        catch (IOException e) {}
        leftico = new ImageIcon(icon.getScaledInstance(50, 50, Image.SCALE_SMOOTH));
        add(new JLabel(leftico),diaconst);

        diaconst.gridx = 1;
        add(rowGear, diaconst);

        ////////////////////////////////////////////////////////////////////////Selector de aerodinamica
        rowAero.setLayout(new GridBagLayout());
        rowconst = new GridBagConstraints();
        rowconst.gridx = 0;
        rowconst.gridy = 0;
        rowconst.anchor = GridBagConstraints.NORTHWEST;
        rowconst.gridwidth = 4;
        rowAero.add(new JLabel("Configura la aerodinamica:"), rowconst);

        double in = (setup.getAero())*10;
        int aeroini = (int)in;
        aero = new JSlider (JSlider.HORIZONTAL, 0, 10, aeroini);
        aero.setMajorTickSpacing(5);
        aero.setMinorTickSpacing(1);
        aero.setPaintTicks(true);
        aero.setPaintLabels(true);
        aero.addChangeListener(new AeroSlider(this));
        Double a = aero.getValue()/10.0;
        carga = new JLabel(a.toString());

        rowconst.gridx = 0;
        rowconst.gridy = 1;
        rowconst.gridwidth = 2;
        rowconst.weightx = 0.0;
        rowconst.anchor = GridBagConstraints.CENTER;
        rowAero.add(aero, rowconst);

        rowconst.gridx = 3;
        rowconst.gridy = 1;
        rowconst.gridwidth = 1;
        rowAero.add(carga,rowconst);
        rowAero.setBorder(BorderFactory.createEmptyBorder(0,0,10,0));

        diaconst.gridx = 0;
        diaconst.gridy = row;
        row++;
        try {icon = ImageIO.read(getClass().getResource("../resources/img/icon/aero.png"));}
        catch (IOException e) {}
        leftico = new ImageIcon(icon.getScaledInstance(50, 50, Image.SCALE_SMOOTH));
        add(new JLabel(leftico),diaconst);

        diaconst.gridx = 1;
        add(rowAero, diaconst);

        ////////////////////////////////////////////////////////Selector de Neumaticos
        rowTire.setLayout(new GridBagLayout());
        rowconst = new GridBagConstraints();
        rowconst.gridx = 0;
        rowconst.gridy = 0;
        rowconst.anchor = GridBagConstraints.NORTHWEST;
        rowconst.gridwidth = 4;
        rowTire.add(new JLabel("Selecciona los neumaticos:"), rowconst);

        soft = new JRadioButton("Blandos");                      // Creacion de botones
        soft.setSelected(true);
        soft.setActionCommand("Blandos");
        //soft.addActionListener(new TeamListener(this));
        hard = new JRadioButton("Duros");
        hard.setActionCommand("Duros");
        //hard.addActionListener(new TeamListener(this));

        tires = new ButtonGroup();                                   // Agrupación de botones para el panel
        tires.add(soft);
        tires.add(hard);

        rowconst.gridwidth = 1;
        rowconst.gridx = 0;
        rowconst.gridy = 1;
        rowTire.add(soft, rowconst);

        rowconst.gridx = 1;
        rowconst.gridy = 1;
        rowTire.add(hard, rowconst);
        rowTire.setBorder(BorderFactory.createEmptyBorder(0,0,10,0));

        diaconst.gridx = 0;
        diaconst.gridy = row;
        row++;
        try {icon = ImageIO.read(getClass().getResource("../resources/img/icon/tire.png"));}
        catch (IOException e) {}
        leftico = new ImageIcon(icon.getScaledInstance(50, 50, Image.SCALE_SMOOTH));
        add(new JLabel(leftico),diaconst);

        diaconst.gridx = 1;
        add(rowTire,diaconst);

        ////////////////////////////////////////////////////////////////////////Selector de Gasolina
        rowFuel.setLayout(new GridBagLayout());
        rowconst = new GridBagConstraints();
        rowconst.gridx = 0;
        rowconst.gridy = 0;
        rowconst.anchor = GridBagConstraints.NORTHWEST;
        rowconst.gridwidth = 4;
        rowFuel.add(new JLabel("Cantidad de combustible:"), rowconst);

        int initfuel = (int)setup.getFuel();
        depo = new JSlider (JSlider.HORIZONTAL, 0, 100, initfuel);
        depo.setMajorTickSpacing(25);
        depo.setMinorTickSpacing(1);
        depo.setPaintTicks(false);
        depo.setPaintLabels(true);
        depo.addChangeListener(new FuelSlider(this));
        Integer b = depo.getValue();
        fuel = new JLabel(b.toString()+" l");

        rowconst.gridx = 0;
        rowconst.gridy = 1;
        rowconst.gridwidth = 2;
        rowconst.weightx = 0.0;
        rowconst.anchor = GridBagConstraints.CENTER;
        rowFuel.add(depo, rowconst);

        rowconst.gridx = 3;
        rowconst.gridy = 1;
        rowconst.gridwidth = 1;
        rowFuel.add(fuel,rowconst);
        rowFuel.setBorder(BorderFactory.createEmptyBorder(0,0,10,0));

        diaconst.gridx = 0;
        diaconst.gridy = row;
        row++;
        try {icon = ImageIO.read(getClass().getResource("../resources/img/icon/fuel.png"));}
        catch (IOException e) {}
        leftico = new ImageIcon(icon.getScaledInstance(50, 50, Image.SCALE_SMOOTH));
        add(new JLabel(leftico),diaconst);

        diaconst.gridx = 1;
        add(rowFuel, diaconst);

        //////////////////////////////////////////////////////////////////////Botonera
        exitButton = new JButton("Salir");
        exitButton.addActionListener(new ActionListener()
                                    {
                                            @Override public void actionPerformed( ActionEvent evento )
                                            {
                                                System.exit(0);
                                            }
                                    });
        playButton = new JButton("Jugar");
        playButton.addActionListener(new ActionListener()
                                    {
                                            @Override public void actionPerformed( ActionEvent evento )
                                            {
                                                String cadena = nombre.getText();
                                                setup.setName(cadena);
                                                dispose();
                                                System.out.println("Let's Play!");
                                            }
                                    });

        JPanel botonera = new JPanel();
        botonera.add(playButton);
        botonera.add(exitButton);

        diaconst.gridy = row;
        add(botonera,diaconst);

        setLocation(left, up);
        setSize(wid, hei);
        setAlwaysOnTop(true);
        setResizable(false);
        setModal(true);
        setUndecorated(true);
        setVisible(true);
    }
}

class TeamListener implements ActionListener
{
    SettingDiag dia;
    public TeamListener(SettingDiag d)
    {
        dia = d;
    }
    @Override
    public void actionPerformed(ActionEvent e)
    {
        Image im=null;
        String brand = e.getActionCommand();
        dia.setup.setTeam(brand);
        brand = "resources/img/icon/"+brand+".png";
        try {im = ImageIO.read(getClass().getResource(brand));}
        catch (IOException ex) {}
        ImageIcon ico = new ImageIcon(im.getScaledInstance(50, 50, Image.SCALE_SMOOTH));
        dia.teampic.setIcon(ico);
    }
}

class GearSlider implements ChangeListener
{
    SettingDiag diag;
    public GearSlider(SettingDiag d)
    {
        diag = d;
    }

    @Override
    public void stateChanged(ChangeEvent ce)
    {
        JSlider source = (JSlider)ce.getSource();
        if (!source.getValueIsAdjusting())
        {
	    int box = ((JSlider)ce.getSource()).getValue();
            diag.setup.setGear(box);
        }
    }

}

class AeroSlider implements ChangeListener
{
    SettingDiag diag;
    public AeroSlider(SettingDiag d)
    {
        diag = d;
    }

    @Override
    public void stateChanged(ChangeEvent ce)
    {
        JSlider source = (JSlider)ce.getSource();
        if (!source.getValueIsAdjusting())
        {
	    Double charge = (double)((JSlider)ce.getSource()).getValue();
            charge = charge/10.0;
            diag.carga.setText(charge.toString());
            diag.setup.setAero(charge);
        }
    }

}

class FuelSlider implements ChangeListener
{
    SettingDiag diag;
    public FuelSlider(SettingDiag d)
    {
        diag = d;
    }

    @Override
    public void stateChanged(ChangeEvent ce)
    {
        JSlider source = (JSlider)ce.getSource();
        if (!source.getValueIsAdjusting())
        {
	    Integer charge = ((JSlider)ce.getSource()).getValue();
            diag.fuel.setText(charge.toString()+" l");
            diag.setup.setFuel(charge);
        }
    }

}